## TV
require(ggplot2)
require(scales)

load('agcom_antenna_time.RData')

x_limits <- c(as.Date("2008-01-01"), as.Date("2016-01-31"))
date_admin_elections_12 <- as.Date('2012-05-06')
date_gener_election_13 <- as.Date('2013-02-24')
date_europ_election_14 <- as.Date('2014-05-22')

antenna_time_df$altro <- antenna_time_df$subject == "Altro"

# Add two opposition parties
Encoding(antenna_time_df$subject) <- 'UTF-8'
antenna_time_df$pd <- grepl('Partito Democratico', antenna_time_df$subject, ignore.case = T)
antenna_time_df$fi <- grepl('Forza Italia|Popolo della Libert', antenna_time_df$subject, ignore.case = T)
antenna_time_df$sc <- grepl('scelta civica|mario monti|liste monti', antenna_time_df$subject, ignore.case = T)

m5s_fi_pd_antenna_time_df <- subset(antenna_time_df, m5s == TRUE | pd == TRUE | fi == TRUE | sc == TRUE)
pol_subj_antenna_time_df <- subset(antenna_time_df, pol_subj == TRUE)
# altro_antenna_time_df <- subset(antenna_time_df, altro == TRUE)

m5s_fi_pd_antenna_time_df <- merge(m5s_fi_pd_antenna_time_df[,c("subject","from_date", "to_date",
                                                                "newscast", "seconds")], 
                                   pol_subj_antenna_time_df[,c("from_date", "to_date",
                                                               "newscast", "seconds")], 
                                   by = c("from_date", "to_date", "newscast"),
                                   all = TRUE)

m5s_fi_pd_antenna_time_df$network <-
  ifelse(grepl("TG1|TG2|TG3|RAINEWS", m5s_fi_pd_antenna_time_df$newscast, ignore.case = T), 
         "RAI", "Mediaset")
m5s_fi_pd_antenna_time_df$subject[grepl('movimento', m5s_fi_pd_antenna_time_df$subject, ignore.case = T)] <- 
  "M5S"
m5s_fi_pd_antenna_time_df$subject[grepl('forza italia|popolo', m5s_fi_pd_antenna_time_df$subject, ignore.case = T)] <- 
  "FI"
m5s_fi_pd_antenna_time_df$subject[grepl('partito democratico', m5s_fi_pd_antenna_time_df$subject, ignore.case = T)] <- 
  "PD"
m5s_fi_pd_antenna_time_df$subject[grepl('Scelta Civica|monti', m5s_fi_pd_antenna_time_df$subject, ignore.case = T)] <- 
  "SC"

m5s_fi_pd_antenna_time_df <-
  m5s_fi_pd_antenna_time_df %>%
  dplyr::group_by(from_date, to_date, newscast, subject, seconds.y, network) %>%
  dplyr::mutate(seconds.x = sum(seconds.x))

m5s_fi_pd_antenna_time_df$ratio <- with(m5s_fi_pd_antenna_time_df, seconds.x / seconds.y)

m5s_fi_pd_antenna_time_df <-
  subset(m5s_fi_pd_antenna_time_df, !(newscast %in% c("RAINEWS","TgCOM"))) %>%
  dplyr::group_by(to_date, subject, network) %>%
  dplyr::summarize(mean_ratio = mean(ratio))

m5s_fi_pd_antenna_time_df <- m5s_fi_pd_antenna_time_df[!(
  m5s_fi_pd_antenna_time_df$subject == "M5S" & m5s_fi_pd_antenna_time_df$to_date < as.Date('2012-06-30')
),]
m5s_fi_pd_antenna_time_df$network <- factor(m5s_fi_pd_antenna_time_df$network,
                                            levels = c("RAI", "Mediaset"))
m5s_fi_pd_antenna_time_df$subject <- factor(m5s_fi_pd_antenna_time_df$subject,
                                            levels = c("FI", "PD", "SC", "M5S"))

# Mean campaign
campaign_days <- 180
findMean <- function (df, party, campaign_days) {
  return(mean(subset(df, subject == party & 
                       to_date > (date_gener_election_13 - campaign_days) &
                       to_date < date_gener_election_13)$mean_ratio, na.rm=TRUE))
}
campaign_mean <- data.frame(party = c("M5S", "PD", "FI", "SC"),
                            mean = c(findMean(m5s_fi_pd_antenna_time_df,"M5S",campaign_days),
                                     findMean(m5s_fi_pd_antenna_time_df,"PD",campaign_days),
                                     findMean(m5s_fi_pd_antenna_time_df,"FI",campaign_days),
                                     findMean(m5s_fi_pd_antenna_time_df,"SC",campaign_days)))

# title="Airtime dedicated to Forza Italia, PD and M5S by RAI and Mediaset"
plot( 
  ggplot() +
  scale_x_date(limits = x_limits) +
  geom_point(data=subset(m5s_fi_pd_antenna_time_df), 
             aes(x=to_date, y=mean_ratio, colour=subject), alpha = 0.3) +
  geom_smooth(data=subset(m5s_fi_pd_antenna_time_df), 
              aes(x=to_date, y=mean_ratio, colour=subject, linetype = network), se = FALSE) +
  geom_vline(xintercept = as.numeric(date_admin_elections_12), 
             linetype = "dotted") +
  geom_vline(xintercept = as.numeric(date_gener_election_13), 
             linetype = "dotted") +
  geom_vline(xintercept = as.numeric(date_europ_election_14), 
             linetype = "dotted") +
  scale_y_continuous(labels = scales::percent) +
  scale_linetype_manual(values=c(1,6),
                        guide = guide_legend(override.aes = list(color = "grey"))) +
  labs( 
       x=NULL, y="Over total time dedicated to parties") +
  guides(colour=guide_legend(title='party')) +
  theme(legend.position = 'bottom') +
  geom_text(data = data.frame(x = c(date_admin_elections_12, date_gener_election_13, date_europ_election_14) + 30,
                              y = 0.7,
                              label =  c('(1)', '(2)', '(3)')),
            aes(x, y, label  = label))
  )
  
